//
//  XLScheduleSearchBarView.m
//  industry
//
//  Created by xl on 2020/12/18.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLScheduleSearchBarView.h"
#import "XLTextCalculateHelper.h"
#import "XLScheduleViewProtocol.h"

@interface XLScheduleSearchBarView ()<UITextFieldDelegate>

@property (nonatomic, strong) XLView *bgView;
@property (nonatomic, strong) XLButton *searchBtn;
@property (nonatomic, strong) XLTextField *textField;

@end
@implementation XLScheduleSearchBarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        [_bgView addSubview:self.textField];
        [_bgView addSubview:self.searchBtn];
    }
    return self;
}

#pragma mark - Public
- (void)changeTextFieldPlaceHolder:(NSString *)content {
    _textField.placeholder = content;
    _textField.text = nil;
}


#pragma mark - Public
- (NSString *)getCurrentSearchText {
    return self.textField.text;
}

#pragma mark - UITextFieldDelegate
- (void)textFieldChange:(XLTextField *)textField {
    XLSend(self.baseController.cView, XLScheduleViewProtocol, beginSearchBar);
}

#pragma mark - UIButton 点击事件
- (void)clickSearch {
    XLSend(self.baseController.cView, XLScheduleViewProtocol, beginSearchBar);
    [self endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_textField resignFirstResponder];
    return YES;
}

// 开始编辑的时候重置为靠左
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [XLTextCalculateHelper textField:textField shouldChangeCharactersInRange:range replacementString:string textLimit:10];
}

#pragma mark - Setter/getter
- (XLView *)bgView {
    if (!_bgView) {
        _bgView = [[XLView alloc] initWithFrame:CGRectMake(30, 0, self.width - 60, 50)];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 12.0f;
        _bgView.layer.shadowColor = [UIColor colorWithHex:0x000000 alpha:0.06].CGColor;
        _bgView.layer.shadowOffset = CGSizeMake(0,4);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.shadowRadius = 10;
    }
    return _bgView;
}

- (XLTextField *)textField {
    if (!_textField) {
        _textField = [[XLTextField alloc] initWithFrame:CGRectMake(15, 5, self.width - 30*2 - 72, self.height - 10)];
        _textField.backgroundColor = [UIColor whiteBgColor];
        _textField.textColor = [UIColor blackFontColor];
        _textField.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        _textField.delegate = self;
        _textField.placeholder = @"搜索待办事项";
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.enablesReturnKeyAutomatically = YES;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

- (XLButton *)searchBtn {
    if (!_searchBtn) {
        _searchBtn = [[XLButton alloc] initWithFrame:CGRectMake(self.bgView.width - 42, (self.bgView.height - 34)/2.0, 34, 34)];
        _searchBtn.backgroundColor = [UIColor blueBgColor];
        _searchBtn.layer.cornerRadius = 10.0f;
        _searchBtn.layer.masksToBounds = YES;
        [_searchBtn setImage:XLIMAGE(@"search") forState:UIControlStateNormal];
        [_searchBtn addTarget:self action:@selector(clickSearch) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

@end
